package com.kc.sjwh.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.bo.PdtBo;
import com.kc.sjwh.entity.bo.UserBo;
import com.kc.sjwh.entity.po.Pdt;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.service.PdtService;

/**
 * pdt action
 * @author zhangguihua(hua7381@163.com)
 * @date 2017年10月17日
 */
@Controller
@RequestMapping("pdt")
public class PdtAction extends BaseAction {
	static Logger logger = Logger.getLogger(PdtAction.class);
	
	@Autowired
	PdtService service;
	
	/**
	 * 打开列表页
	 * @param req
	 * @return
	 */
	@RequestMapping("list")
	public String list(HttpServletRequest req) {
		return "pdt/pdtList";
	}
	
	/**
	 * 分页查询
	 * @param req
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getPage")
	public String getPage(HttpServletRequest req, int pageSize, int pageNum) {
		Map<String, String> m = super.getParamMap(req);
		
		if(!SecurityUtils.getSubject().hasRole("admin")) {
			m.put("dwdm", getLoginUser().getSsdwdm());
		}
		
		Page<Pdt> page = service.getPage(pageSize, pageNum, m);
		
		StringBuffer sb = new StringBuffer();
		for(Pdt one : page.getList()) {//数据行
			PdtBo bo = (PdtBo)one;
			sb.append("<tr>");
			sb.append("<td>"+chk(bo.getSbbh())+"</td>");
			sb.append("<td>"+chk(bo.getSbmc())+"</td>");
			sb.append("<td>"+chk(bo.getZzjgmc())+"</td>");
			sb.append("<td>"+chk(bo.getBdrymc())+"</td>");
			sb.append("<td>"+chk(bo.getLxdh())+"</td>");
			sb.append("<td>"+chk(bo.getSbzt())+"</td>");
			sb.append("<td>"+chk(bo.getSblxms())+"</td>");
			sb.append("<td>");
			sb.append("	<a href='form?id="+bo.getSbbh()+"'>编辑</a> &nbsp;");
			sb.append("	<a onclick=\"deletes('"+bo.getSbbh()+"')\">删除</a>");
			sb.append("</td>");
			sb.append("</tr>");
		}
		
		//返回结果
		Map data = new HashMap();
		data.put("total", page.getTotal());
		data.put("html", sb.toString());
		return JSON.toJSONString(new AjaxRes(data));
	}
	
	private Object chk(Object val) {
		return val == null ? "-" : val;
	}
	
	/**
	 * 打开表单(新增或修改)
	 * @param id
	 */
	@RequestMapping("form")
	public String form(HttpServletRequest req, String id) {
		req.setAttribute("act", id==null?"add":"update");
		req.setAttribute("actName", id==null?"新增":"修改");
		req.setAttribute("one", service.form(id));
		req.setAttribute("loginUser", super.getLoginUser());
		return "pdt/pdtForm";
	}
	
	/**
	 * 保存（新增或修改）
	 * @param one
	 * @param act add/update
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(Pdt one, String act) {
		if("add".equals(act)) {
			UserBo user = getLoginUser();
			one.setCjr(user.getMc());
			if(StringUtils.isEmpty(one.getZzjgdm())) {
				one.setZzjgdm(user.getSsdwdm());
				one.setZzjgmc(user.getSsdwmc());
			}
			service.add(one);
		} else {
			one.setGxr(getLoginUser().getMc());
			service.update(one);
		}
		return JSON.toJSONString(new AjaxRes());
	}
	
	/**
	 * 删除
	 * @param req
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest req, String id) {
		service.delete(id);
		return JSON.toJSONString(new AjaxRes());
	}
}
